home *** CD-ROM | disk | FTP | other *** search
/ Quick PC 61 / Quick PC 61.iso / I386 / WMS.CAB / wmsServerHash.inc < prev    next >
Encoding:
Text File  |  2003-02-21  |  6.0 KB  |  191 lines

  1. <%
  2. '+-------------------------------------------------------------------------
  3. '
  4. '  Microsoft Windows Media
  5. '  Copyright (C) Microsoft Corporation. All rights reserved.
  6. '
  7. '  File:        wmsServerHash.inc
  8. '
  9. '  Contents:    Ops to hash and unhash the server name passed in the query string
  10. '
  11. '  Comments:    Included by wmsconnect.inc
  12. '
  13. '--------------------------------------------------------------------------
  14.  
  15. %><script language="JScript" RUNAT=Server>
  16. ////////////////////////////////////////////////////////////////////
  17. function GetTimestamp()
  18. {
  19.     var d;
  20.     var szRetVal;
  21.     
  22.     d = new Date();
  23.     
  24.     szRetVal = new String( d.getSeconds() );
  25.     szRetVal += d.getMilliseconds();
  26.     return( szRetVal );
  27. }
  28. </script><%
  29.  
  30. on error resume next
  31. Dim g_objDictionary
  32. g_objDictionary = empty
  33.  
  34. '//////////////////////////////////////////////////////////////////////////  
  35. private Function CreateHashKey()
  36.     Dim strVal
  37.     Dim strGuid
  38.     Dim objGuidGen
  39.     Set objGuidGen = Server.CreateObject( "Scriptlet.TypeLib" )
  40.     strGuid = objGuidGen.Guid
  41.     strGuid = Replace( strGuid, "{", "X" )
  42.     strGuid = Replace( strGuid, "}", "Y" )
  43.     strGuid = Replace( strGuid, "-", "Z" )
  44.  
  45.     strVal = Mid( strGuid, 2, 8 ) & GetTimestamp()
  46. '    strVal = strGuid
  47.     CreateHashKey = strVal
  48. End Function
  49.  
  50. '//////////////////////////////////////////////////////////////////////////  
  51. '//
  52. '//  Wrapper for ReverseHashServerName
  53. '//
  54. public Function ServerNameToHashKey( strServerName, bAddIfNonexistent )
  55.     Dim strKey
  56.     strKey = ""
  57.  
  58.     if( 0 >= Len( strServerName ) ) then
  59.         ServerNameToHashKey = ""
  60.         Exit Function
  61.     end if
  62.  
  63.     if( 0 = StrComp( "localhost", strServerName, vbTextCompare ) ) then
  64.         strServerName = s_WMSAdmin.GetCanonicalComputerName( "localhost", WMS_ComputerNameNetBIOS )
  65.     end if
  66.     
  67.     strServerName = LCase( strServerName )
  68.     strKey = ReverseHashServerName( strServerName )
  69.     if( bAddIfNonexistent and ( 0 = Len( strKey ) ) ) then
  70.         strKey = CreateHashKey
  71. '        strKey = "hk" & Left( Request.ServerVariables( "INSTANCE_ID" ), 4 ) & GetTimestamp()
  72.         if( IsEmpty( g_objDictionary ) ) then
  73.             Set g_objDictionary = Session( "SvrDictionary" )
  74.         end if
  75.         Do While( g_objDictionary.Exists( strKey ) )
  76.             strKey = "hk" & GetTimestamp()
  77.         Loop
  78.         g_objDictionary.Add CStr( strKey ), CStr( strServerName )
  79.     end if
  80.     
  81.     ServerNameToHashKey = strKey
  82. End Function
  83.  
  84.  
  85. '//////////////////////////////////////////////////////////////////////////  
  86. public Sub RemoveServerFromHash( szHashKey )
  87.     if( 0 < Len( szHashKey ) ) then
  88.         if( IsEmpty( g_objDictionary ) ) then
  89.             Set g_objDictionary = Session( "SvrDictionary" )
  90.         end if
  91.         if( g_objDictionary.Exists( CStr( szHashKey ) ) ) then
  92.             g_objDictionary.Remove( CStr( szHashKey ) )
  93.         end if
  94.     end if
  95. End Sub
  96.  
  97.  
  98. '//////////////////////////////////////////////////////////////////////////  
  99. '
  100. ' Resolve a hash key to the original servername
  101. '
  102. public Function ServerHashKeyToName( strServerHashKey )  
  103.     on error resume next
  104.     if( IsEmpty( g_objDictionary ) ) then
  105.         Set g_objDictionary = Session( "SvrDictionary" )
  106.     end if
  107.     if( g_objDictionary.Exists( SafeUnescape( strServerHashKey ) ) ) then
  108.         ServerHashKeyToName = CStr( g_objDictionary.Item( SafeUnescape( strServerHashKey ) ) )
  109.     else
  110.         ServerHashKeyToName = ""
  111.     end if
  112. End Function
  113.  
  114.  
  115. '//////////////////////////////////////////////////////////////////////////  
  116. ' Used for debugging only
  117. public Sub DumpServerHash()
  118.     exit sub ' remove this line if you wish to make use of this subroutine
  119.     Dim serverDictionary    
  120.     Dim lenDict
  121.     Dim rgKeys
  122.     Dim rgItems
  123.     
  124.     if( IsEmpty( g_objDictionary ) ) then
  125.         Set g_objDictionary = Session( "SvrDictionary" )
  126.     end if
  127.     if( IsEmpty( g_objDictionary ) ) then
  128.         Exit Sub
  129.     end if
  130.     
  131.     lenDict = g_objDictionary.Count
  132.     rgKeys = g_objDictionary.Keys
  133.     rgItems = g_objDictionary.Items
  134.     
  135.     Response.Write( "Debug info: <br><br><table cellspacing=1 cellpadding=4 border=1 cols=3><th colspan=3>Servers (" & lenDict & ") in hash table</th><tbody>" )
  136.     i = 0
  137.     For i = 0 to lenDict - 1
  138.         Response.Write( "<tr><td>" & i & ") " & "</td><td>" & Server.HTMLEncode( rgKeys( i ) ) & "</td><td>" & Server.HTMLEncode( rgItems( i ) ) & "</td></tr>" )
  139.     Next
  140.     Response.Write( "</tbody></table>" )
  141.     serverDictionary = ""
  142. End Sub
  143.  
  144.  
  145. '//////////////////////////////////////////////////////////////////////////  
  146. '// Private methods
  147. '//////////////////////////////////////////////////////////////////////////  
  148.  
  149.  
  150. '//////////////////////////////////////////////////////////////////////////  
  151. private Function ReverseHashServerName( strServerName )
  152.     Dim rgKeys
  153.     Dim rgItems
  154.     Dim strEachServerName
  155.     Dim i
  156.  
  157.     on error resume next
  158.     dbgNumItemsVisited = 0
  159.  
  160.     if( IsEmpty( g_objDictionary ) ) then
  161.         Set g_objDictionary = Session( "SvrDictionary" )
  162.     end if
  163.     if( IsEmpty( g_objDictionary ) ) then
  164.         ReverseHashServerName = ""
  165.         Exit Function
  166.     end if
  167.     rgKeys = g_objDictionary.Keys
  168.     rgItems = g_objDictionary.Items
  169.  
  170.     For i = 0 to g_objDictionary.Count - 1
  171.         dbgNumItemsVisited = dbgNumItemsVisited + 1
  172.         strEachServerName = CStr( rgItems( i ) )
  173.         if( ( 0 < Len( strEachServerName ) ) and ( 0 = StrComp( strServerName, strEachServerName, vbTextCompare ) ) ) then
  174.             ReverseHashServerName = rgKeys( i )
  175.             Exit Function
  176.         end if
  177.     Next
  178.  
  179. '    Session( "dbg" ) = "ReverseHashServerName failed to find " & strServerName & "<br>"
  180.     ReverseHashServerName = ""
  181. End Function
  182.  
  183. '////////////////////////////////////////////////////////////////
  184. '
  185. ' Free the various globals we created by server-side-including this file
  186. '
  187. Sub WMSServerHashASPCleanup
  188.     on error resume next
  189.     g_objDictionary = nothing
  190. End Sub
  191. %>